import 'package:flutter/material.dart';

/// 数量
class NumberCounter extends StatefulWidget {
  final double height;
  final double width;
  final double iconWidth;
  final int number;
  final int minNumber;
  final ValueChanged addValueChanged;
  final ValueChanged removeValueChanged;
  final ValueChanged updateValueChanged;

  const NumberCounter({
    Key key,
    this.height = 24,
    this.width = 40,
    this.iconWidth = 40,
    this.number = 1,
    this.minNumber = 1,
    this.addValueChanged,
    this.removeValueChanged,
    this.updateValueChanged,
  }) : super(key: key);

  @override
  _NumberCounterState createState() => _NumberCounterState();
}

class _NumberCounterState extends State<NumberCounter> {
  TextEditingController _editingController;

  @override
  void initState() {
    super.initState();
    _editingController = TextEditingController(text: '${widget.number}');
  }

  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4),
        border: Border.all(width: 1, color: Colors.black12),
      ),
      height: widget.height,
      child: Row(
        children: [
          _buildIconButton(icon: Icons.remove, isAdd: false),
          _buildTextField(),
          _buildIconButton(icon: Icons.add, isAdd: true),
        ],
      ),
    );
  }

  Widget _buildIconButton({IconData icon, bool isAdd}) {
    return Container(
      width: widget.width,
      child: IconButton(
        padding: EdgeInsets.zero,
        icon: Icon(icon),
        onPressed: () {
          // 加、减、更新值
          var number = int.tryParse(_editingController.text) ?? widget.minNumber;
          if (!isAdd && number == widget.minNumber) return;
          if (isAdd) {
            number++;
            widget.addValueChanged?.call(number);
          } else {
            number--;
            widget.removeValueChanged?.call(number);
          }
          _editingController.text = '$number';
          widget.updateValueChanged?.call(number);
        },
      ),
    );
  }

  Widget _buildTextField() {
    return Container(
      decoration: BoxDecoration(
        border: Border.symmetric(
          vertical: BorderSide(width: 1, color: Colors.black12),
        ),
      ),
      width: widget.width,
      child: TextField(
        controller: _editingController,
        keyboardType: TextInputType.number,
        textAlign: TextAlign.center,
        enableInteractiveSelection: false,
        decoration: InputDecoration(
          contentPadding: EdgeInsets.zero,
          isDense: true,
          focusedBorder: InputBorder.none,
          enabledBorder: InputBorder.none,
          border: InputBorder.none,
        ),
        onChanged: (value) {
          // 更新值
          final number = int.tryParse(value) ?? widget.minNumber;
          widget.updateValueChanged?.call(number);
        },
      ),
    );
  }
}
